import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:photo_view/photo_view.dart';
import 'package:craze/common/index.dart';

class ImagePreviewPage extends StatelessWidget {
  final String imagePath;
  final bool isAsset;
  final String? heroTag;

  const ImagePreviewPage({
    super.key,
    required this.imagePath,
    this.isAsset = true,
    this.heroTag,
  });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Stack(
        children: [
          // 图片预览
          Center(
            child: Hero(
              tag: heroTag ?? imagePath,
              child: PhotoView(
                imageProvider:
                    isAsset
                        ? AssetImage(imagePath)
                        : FileImage(File(imagePath)) as ImageProvider,
                minScale: PhotoViewComputedScale.contained,
                maxScale: PhotoViewComputedScale.covered * 2,
                backgroundDecoration: const BoxDecoration(color: Colors.black),
                loadingBuilder:
                    (context, event) => Center(
                      child: CircularProgressIndicator(
                        value:
                            event == null
                                ? 0
                                : event.cumulativeBytesLoaded /
                                    (event.expectedTotalBytes ?? 1),
                        color: Colors.white,
                      ),
                    ),
              ),
            ),
          ),

          // 返回按钮
          SafeArea(
            child: Padding(
              padding: EdgeInsets.all(16.w),
              child: GestureDetector(
                onTap: () => Navigator.of(context).pop(),
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.black.withOpacity(0.5),
                    shape: BoxShape.circle,
                  ),
                  padding: EdgeInsets.all(8.w),
                  child: Icon(Icons.close, color: Colors.white, size: 24.sp),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
